//
//  MineController.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/3/9.
//
//  我的控制器
//  dispatch_semaphore / NSConditionLock

#import "MineController.h"
#import "BYOrderItemModel.h"
#import "BYMineInfoOrderCell.h"
#import "DBUser.h"

#import "BYLoginController.h"

static NSString *kMineInfoOrderCellId = @"kMineInfoOrderCellId";

@interface MineController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) BYTableView   *tableView;
@property (nonatomic, strong) BYButton      *quiteButton;

@end

@implementation MineController

#pragma mark - Event

- (void)quiteLoginEvent {
    NSLog(@"发送退出通知");
    [[NSNotificationCenter defaultCenter] postNotificationName:kFSNotificationNameLoginOutSuccess object:nil];
}

- (void)loginEvent {
    BYLoginController *loginController = [[BYLoginController alloc] initWithNavigationType:BYNavigationTypeHide];
    loginController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:loginController animated:YES completion:^{
    }];
}

- (void)singinEvent {
    NSLog(@"注册事件");
}

- (void)quiteEvent {
    NSLog(@"退出登录事件");
}

#pragma mark - NotificationEvent

- (void)loginSuccessNotification {
    // 取出登录状态的字典
    NSMutableArray *loginArray = [kUserDefaultsSington valueForKey:kIsLoginArray];
    NSMutableDictionary *loginDict = [loginArray objectAtIndex:0];
    BOOL isLogin = [loginDict objectForKey:@"isLogin"];
    NSString *userType = [loginDict objectForKey:@"user_type"];
    NSLog(@"登录数据-Arry：%@", loginArray);
    NSLog(@"登录数据-Dict：%@", loginDict);
    NSLog(@"登录信息：%@ == %@", @(isLogin) , userType);
    
    // 用户是否登录
    if ([AppConfig isLogin]) {
        [self.view addSubview:self.quiteButton];
    }else {
        [self.quiteButton removeFromSuperview];
    }
    
//    if ([login isEqualToString:@"login"]) {
//        if ([userType isEqualToString:@"管理员"]) {
//            [self.view addSubview:self.mangeView];
//        }
//    } else {
//        [self.exitButton removeFromSuperview];
//    }
    
    
}


- (void)quitNotification {
    
    
}

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_VIEW_COLOR;
    [self.view addSubview:self.tableView];
    
    // 登录用户添加退出按钮
    if([AppConfig isLogin]) {
        [self.view addSubview:self.quiteButton];
    }
    
    [kNotificationCenterSington addObserver:self selector:@selector(loginSuccessNotification) name:kFSNotificationNameLoginSuccess object:nil];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if([AppConfig isLogin]) {
        [self.quiteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(kAutoWithSize(10));
            make.right.equalTo(self.view).offset(-kAutoWithSize(10));
            make.height.mas_offset(kAutoWithSize(50));
            make.bottom.equalTo(self.view).offset(-(kAutoWithSize(50)+kSafeAreaBottomHeight+kTabBarHeight));
        }];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    BYMineInfoOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineInfoOrderCellId forIndexPath:indexPath];
    
    WS(weakSelf)
    cell.signinBlock = ^{
        [weakSelf singinEvent];
    };
    
    cell.loginBlock = ^{
        [weakSelf loginEvent];
    };

    [cell setData];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [BYMineInfoOrderCell cellHeight];
}

#pragma mark - LazyGet

- (BYTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BYTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kSafeAreaTopHeight-kSafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = BACKGROUND_VIEW_COLOR;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[BYMineInfoOrderCell class] forCellReuseIdentifier:kMineInfoOrderCellId];
    }
    return _tableView;
}

- (BYButton *)quiteButton {
    if (!_quiteButton) {
        _quiteButton = [[BYButton alloc] initWithFrame:CGRectZero];
        _quiteButton.titleLabel.font = MZFont(kFont(14));
        [_quiteButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [_quiteButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        [_quiteButton setBackgroundColor:PUBLIC_ORANGE_COLOR forState:UIControlStateNormal];
        [_quiteButton setBackgroundColor:PUBLIC_ORANGE_COLOR forState:UIControlStateSelected];
        [_quiteButton addTarget:self action:@selector(quiteEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _quiteButton;
}


@end
